import React, {Component} from 'react';
import { Router, Route, Redirect, IndexRoute, browserHistory, hashHistory } from 'react-router';
import store from '../Redux/Store/Store';
import index from '../Component/index'; 

class Roots extends Component {
    render() {
        return (
            <div>{this.props.children}</div>
        );
    }
}

const history = process.env.NODE_ENV !== 'production' ? browserHistory : hashHistory;

const main = (location, cb) => {
    require.ensure([], require => {
        cb(null, require('../Component/main').default)
    },'main')
}

const order = (location, cb) => {
    require.ensure([], require => {
        cb(null, require('../Component/order').default)
    },'order')
}

const service = (location, cb) => {
    require.ensure([], require => {
        cb(null, require('../Component/service').default)
    },'service')
}

const me = (location, cb) => {
    require.ensure([], require => {
        cb(null, require('../Component/me').default)
    },'me')
}

const issuing = (location, cb) => {
    require.ensure([], require => {
        cb(null, require('../Component/issuing').default)
    },'issuing')
}

const clearance = (location, cb) => {
    require.ensure([], require => {
        cb(null, require('../Component/clearance').default)
    },'clearance')
}

const transport = (location, cb) => {
    require.ensure([], require => {
        cb(null, require('../Component/transport').default)
    },'transport')
}

const cost = (location, cb) => {
    require.ensure([], require => {
        cb(null, require('../Component/cost').default)
    },'cost')
}

const orderdetail = (location, cb) => {
    require.ensure([], require => {
        cb(null, require('../Component/orderdetail').default)
    },'orderdetail')
}

const lost = (location, cb) => {
    require.ensure([], require => {
        cb(null, require('../Component/lost').default)
    },'lost')
}

const signup = (location, cb) => {
    require.ensure([], require => {
        cb(null, require('../Component/signup').default)
    },'signup')
}

const requireAuth = (nextState, replace) => {
    if (!store.getState().authReducer.isLoggedIn) {
        replace({ pathname: '/' })
    }
}

const RouteConfig = (
    <Router history={history}>
        <Route path="/" component={Roots}>
            <IndexRoute component={index} />
            <Route path="index" component={index} />
            <Route path="main" getComponent={main} onEnter={requireAuth}>
                <Route path="order" getComponent={order} />
                <Route path="service" getComponent={service} />
                <Route path="me" getComponent={me} />
            </Route>
            <Route path="issuing" getComponent={issuing} />
            <Route path="clearance" getComponent={clearance} />
            <Route path="transport" getComponent={transport} />
            <Route path="cost" getComponent={cost} />
            <Route path="orderdetail" getComponent={orderdetail} />
            <Route path="lost" getComponent={lost} />
            <Route path="signup" getComponent={signup} />
            <Redirect from='*' to='/'  />
        </Route>
    </Router>
);

export default RouteConfig;